﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Model;
using HT.DBUtility;

namespace HT.DAL
{
    public class Dal_Xskdh_1
    {

        /// <summary>
        /// 从商品档案表中获取信息
        /// </summary>
        /// <param name="spbh"></param>
        /// <param name="filed"></param>
        /// <returns></returns>
        public string GetDataByFiled(string spbh, string filed)
        {

            StringBuilder strSql = new StringBuilder();

            strSql.Append("select " + filed + "  from  dbo.spdaH  where spbh='" + spbh + "' ");

            string name = "";

            DataTable dt = SQLHelper.ExecuteDt(strSql.ToString());

            if (dt != null && dt.Rows.Count > 0)
            {
                name = dt.Rows[0][0].ToString();
            }

            return name;
        }

        /// <summary>
        /// 从 条件表中获取信息
        /// </summary>
        /// <param name="content">查询内容</param>
        /// <param name="tableName">表名</param>
        /// <param name="condition">条件(含 where 部分)</param>
        /// <returns></returns>
        public string GetDataByFiled(string content, string tableName, string condition)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select " + content + "  from  " + tableName + condition);
            string name = "";

            DataTable dt = SQLHelper.ExecuteDt(strSql.ToString());

            if (dt != null && dt.Rows.Count > 0)
            {
                name = dt.Rows[0][0].ToString();
            }
            else
            {
                name = "0";
            }


            return name;

        }

        /// <summary>
        /// 从 条件表中获取信息
        /// </summary>

        /// <param name="filed">想获取值 字段</param>
        /// <param name="tableName">表名</param>
        /// <param name="key">条件</param>
        /// <param name="condition">条件值</param>
        /// <returns></returns>
        public string GetDataByFiled(string filed, string tableName, string key, string condition)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select " + filed + "  from  " + tableName + "  where " + key + "='" + condition + "' ");
            string name = "";

            DataTable dt = SQLHelper.ExecuteDt(strSql.ToString());

            if (dt != null && dt.Rows.Count > 0)
            {
                name = dt.Rows[0][0].ToString();
            }
            return name;
        }

        /// <summary>
        /// 获取最新 单据流水号
        /// </summary>
        /// <param name="type">0 销售</param>
        /// <returns></returns>
        public int GetNewDjlshID(int type)
        {
            int number = 0;
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select top 1 djlsh from xskdh order by djlsh desc");

            string name = "";

            DataTable dt = SQLHelper.ExecuteDt(strSql.ToString());

            if (dt != null && dt.Rows.Count > 0)
            {
                name = dt.Rows[0][0].ToString();
            }

            if (name != "")
            {
                try
                {
                    number = int.Parse(name);
                }
                catch { }
            }
            number++;
            return number;
        }


        /// <summary>
        /// 保存销售单
        /// </summary>
        /// <param name="sqlList"></param>
        /// <returns></returns>
        public int SaveXskdData(List<string> sqlList)
        {

            int s1 = SQLHelper.ExecuteSqlTran(sqlList);
            return s1;
        }



        /// <summary>
        /// 得到一个对象实体(xskdh)
        /// </summary>
        /// <returns></returns>
        public xskdh GetXskdhModel(string xsdh)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 DjLsh,DjBtZdh,DjFtZdh,DjStZdh,DjState,DjCount,rq,sj,djzt,xsdh,Bmbh,Bmmc,yyybh,yyymc,khbh,khmc,hyk,hykxf,slhj,jehj,xj,zrje,bqjf,Zjf,bz,djyh,djsj,xgyh,xgsj,cszt,yl,ssjehj,wx,zfb,djq,yhk  from xskdh ");
            strSql.Append(" where xsdh='" + xsdh + "'");

            xskdh model = new xskdh();

            DataTable dt = SQLHelper.ExecuteDt(strSql.ToString());
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["DjLsh"] != null && dt.Rows[0]["DjLsh"].ToString() != "")
                {
                    model.DjLsh = int.Parse(dt.Rows[0]["DjLsh"].ToString());
                }
                if (dt.Rows[0]["DjBtZdh"] != null && dt.Rows[0]["DjBtZdh"].ToString() != "")
                {
                    model.DjBtZdh = int.Parse(dt.Rows[0]["DjBtZdh"].ToString());
                }
                if (dt.Rows[0]["DjFtZdh"] != null && dt.Rows[0]["DjFtZdh"].ToString() != "")
                {
                    model.DjFtZdh = int.Parse(dt.Rows[0]["DjFtZdh"].ToString());
                }
                if (dt.Rows[0]["DjStZdh"] != null && dt.Rows[0]["DjStZdh"].ToString() != "")
                {
                    model.DjStZdh = int.Parse(dt.Rows[0]["DjStZdh"].ToString());
                }
                if (dt.Rows[0]["DjState"] != null && dt.Rows[0]["DjState"].ToString() != "")
                {
                    model.DjState = dt.Rows[0]["DjState"].ToString();
                }
                if (dt.Rows[0]["DjCount"] != null && dt.Rows[0]["DjCount"].ToString() != "")
                {
                    model.DjCount = int.Parse(dt.Rows[0]["DjCount"].ToString());
                }
                model.rq = dt.Rows[0]["rq"].ToString();
                model.sj = dt.Rows[0]["sj"].ToString();
                if (dt.Rows[0]["djzt"] != null && dt.Rows[0]["djzt"].ToString() != "")
                {
                    model.djzt = dt.Rows[0]["djzt"].ToString();
                }
                if (dt.Rows[0]["xsdh"] != null && dt.Rows[0]["xsdh"].ToString() != "")
                {
                    model.xsdh = dt.Rows[0]["xsdh"].ToString();
                }
                if (dt.Rows[0]["Bmbh"] != null && dt.Rows[0]["Bmbh"].ToString() != "")
                {
                    model.Bmbh = dt.Rows[0]["Bmbh"].ToString();
                }
                if (dt.Rows[0]["Bmmc"] != null && dt.Rows[0]["Bmmc"].ToString() != "")
                {
                    model.Bmmc = dt.Rows[0]["Bmmc"].ToString();
                }
                if (dt.Rows[0]["yyybh"] != null && dt.Rows[0]["yyybh"].ToString() != "")
                {
                    model.yyybh = dt.Rows[0]["yyybh"].ToString();
                }
                if (dt.Rows[0]["yyymc"] != null && dt.Rows[0]["yyymc"].ToString() != "")
                {
                    model.yyymc = dt.Rows[0]["yyymc"].ToString();
                }
                if (dt.Rows[0]["khbh"] != null && dt.Rows[0]["khbh"].ToString() != "")
                {
                    model.khbh = dt.Rows[0]["khbh"].ToString();
                }
                if (dt.Rows[0]["khmc"] != null && dt.Rows[0]["khmc"].ToString() != "")
                {
                    model.khmc = dt.Rows[0]["khmc"].ToString();
                }
                if (dt.Rows[0]["hyk"] != null && dt.Rows[0]["hyk"].ToString() != "")
                {
                    model.hyk = dt.Rows[0]["hyk"].ToString();
                }
                if (dt.Rows[0]["hykxf"] != null && dt.Rows[0]["hykxf"].ToString() != "")
                {
                    model.hykxf = decimal.Parse(dt.Rows[0]["hykxf"].ToString());
                }
                if (dt.Rows[0]["slhj"] != null && dt.Rows[0]["slhj"].ToString() != "")
                {
                    model.slhj = decimal.Parse(dt.Rows[0]["slhj"].ToString());
                }
                if (dt.Rows[0]["jehj"] != null && dt.Rows[0]["jehj"].ToString() != "")
                {
                    model.jehj = decimal.Parse(dt.Rows[0]["jehj"].ToString());
                }
                if (dt.Rows[0]["xj"] != null && dt.Rows[0]["xj"].ToString() != "")
                {
                    model.xj = decimal.Parse(dt.Rows[0]["xj"].ToString());
                }
                if (dt.Rows[0]["zrje"] != null && dt.Rows[0]["zrje"].ToString() != "")
                {
                    model.zrje = decimal.Parse(dt.Rows[0]["zrje"].ToString());
                }
                if (dt.Rows[0]["bqjf"] != null && dt.Rows[0]["bqjf"].ToString() != "")
                {
                    model.bqjf = decimal.Parse(dt.Rows[0]["bqjf"].ToString());
                }
                if (dt.Rows[0]["Zjf"] != null && dt.Rows[0]["Zjf"].ToString() != "")
                {
                    model.Zjf = decimal.Parse(dt.Rows[0]["Zjf"].ToString());
                }
                if (dt.Rows[0]["bz"] != null && dt.Rows[0]["bz"].ToString() != "")
                {
                    model.bz = dt.Rows[0]["bz"].ToString();
                }
                if (dt.Rows[0]["djyh"] != null && dt.Rows[0]["djyh"].ToString() != "")
                {
                    model.djyh = dt.Rows[0]["djyh"].ToString();
                }
                if (dt.Rows[0]["djsj"] != null && dt.Rows[0]["djsj"].ToString() != "")
                {
                    model.djsj = DateTime.Parse(dt.Rows[0]["djsj"].ToString());
                }
                if (dt.Rows[0]["xgyh"] != null && dt.Rows[0]["xgyh"].ToString() != "")
                {
                    model.xgyh = dt.Rows[0]["xgyh"].ToString();
                }
                if (dt.Rows[0]["xgsj"] != null && dt.Rows[0]["xgsj"].ToString() != "")
                {
                    model.xgsj = DateTime.Parse(dt.Rows[0]["xgsj"].ToString());
                }
                if (dt.Rows[0]["cszt"] != null && dt.Rows[0]["cszt"].ToString() != "")
                {
                    model.cszt = int.Parse(dt.Rows[0]["cszt"].ToString());
                }
                if (dt.Rows[0]["yl"] != null && dt.Rows[0]["yl"].ToString() != "")
                {
                    model.yl = decimal.Parse(dt.Rows[0]["yl"].ToString());
                }

                if (dt.Rows[0]["ssjehj"] != null && dt.Rows[0]["ssjehj"].ToString() != "")
                {
                    model.ssjehj = decimal.Parse(dt.Rows[0]["ssjehj"].ToString());
                }

                if (dt.Rows[0]["wx"] != null && dt.Rows[0]["wx"].ToString() != "")
                {
                    model.wx = decimal.Parse(dt.Rows[0]["wx"].ToString());
                }
                if (dt.Rows[0]["zfb"] != null && dt.Rows[0]["zfb"].ToString() != "")
                {
                    model.zfb = decimal.Parse(dt.Rows[0]["zfb"].ToString());
                }
                if (dt.Rows[0]["djq"] != null && dt.Rows[0]["djq"].ToString() != "")
                {
                    model.djq = decimal.Parse(dt.Rows[0]["djq"].ToString());
                }
                if (dt.Rows[0]["yhk"] != null && dt.Rows[0]["yhk"].ToString() != "")
                {
                    model.yhk = decimal.Parse(dt.Rows[0]["yhk"].ToString());
                }



                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetPfdBData(string xsdh)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DjLsh,DjBth,DjState,hh,tmh,spbh,spmc,gg,dw,sl,Dj,pfj,hyzk,hyyh,zyhjg,kcsl,je ssje,hxsdh,yyy,xh,jj,ysbh,ysmc,cm ");
            strSql.Append(" FROM xskdB ");
            if (xsdh.Trim() != "")
            {
                strSql.Append(" where hxsdh='" + xsdh + "'");
            }
            return SQLHelper.ExecuteDt(strSql.ToString());
        }
        public DataTable ExecuteDt(string sql)
        {
            return SQLHelper.ExecuteDt(sql);

        }

    }
}
